package com.msmall.community.baby.preparer;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.common.model.Commend;
import com.msmall.community.keyword.model.Keyword;

public class BabyIndexTopPreparer implements ViewPreparer{
	
	private static final Log log = LogFactory.getLog(BabyIndexTopPreparer.class);
	
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			List<Commend> babyIndexTopImages = (List<Commend>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_INDEX_COMMEND_TOP_IMAGES);
			request.setAttribute("babyIndexTopImages", babyIndexTopImages);
			
			List<Commend> babyIndexTopNews = (List<Commend>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_INDEX_COMMEND_TOPNEWS);
			request.setAttribute("babyIndexTopNews", babyIndexTopNews);
			
			List<Commend> babyIndexTopNewsBelow = (List<Commend>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_INDEX_COMMEND_TOPNEWS_BELOW);
			request.setAttribute("babyIndexTopNewsBelow", babyIndexTopNewsBelow);
			
			List<Commend> babyIndexTopNewsBelowImage = (List<Commend>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_INDEX_COMMEND_TOPNEWS_BELOW_IMAGE);
			request.setAttribute("babyIndexTopNewsBelowImage", babyIndexTopNewsBelowImage);
			
			Map<Short, List<Keyword>> maps = (Map<Short, List<Keyword>>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KEYWORD);
			request.setAttribute("babyIndexTopKeywords", maps.get(ConstantDB.KEYWORD_TYPE_SITE_INDEX_TOP));
		}catch(Exception e){
			log.debug(e);
		}
	}
}
